package example.fileupload3;

import grpc.htsoft.fileupload.FileServiceGrpc;
import grpc.htsoft.fileupload.FileUploadRequest;
import grpc.htsoft.fileupload.FileUploadResponse;
import grpc.htsoft.fileupload2.FileChunk;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.io.FileInputStream;
import java.io.IOException;

public class fileuploadclient {
    private final ManagedChannel channel;
    private final FileServiceGrpc.FileServiceStub Stub;

    public fileuploadclient(String host, int port) {
        this.channel = ManagedChannelBuilder.forAddress(host, port)
                .usePlaintext()
                .build();
        Stub = FileServiceGrpc.newStub(channel);
    }

    public void UPLOAD(String path) {
        //服务端返回的数据
        StreamObserver<FileUploadResponse> responseObserver = new StreamObserver<FileUploadResponse>() {
            @Override
            public void onNext(FileUploadResponse value) {
                System.out.println(value.getName());
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
            }

            @Override
            public void onCompleted() {
                System.out.println("File upload completed.");
                channel.shutdownNow();
            }
        };

        //客户端发送的数据
        StreamObserver<grpc.htsoft.fileupload.FileUploadRequest> requestObserver = Stub.upload(responseObserver);

        requestObserver.onNext(grpc.htsoft.fileupload.FileUploadRequest.newBuilder()
                .setMetadata(grpc.htsoft.fileupload.MetaData.newBuilder()
                        .setName("output3")
                        .setType("png").build())
                .build());

        try {
            FileInputStream input = new FileInputStream(path);
            byte[] buffer = new byte[4096]; // Adjust the buffer size if needed
            int size;
            while ((size = input.read(buffer)) > 0) {
                FileUploadRequest uploadRequest = FileUploadRequest.newBuilder()
                        .setFile(grpc.htsoft.fileupload.File.newBuilder()
                                .setContent(com.google.protobuf.ByteString.copyFrom(buffer, 0, size))
                                .build())
                        .build();



                requestObserver.onNext(uploadRequest);
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
            requestObserver.onError(e);
            return;
        }

        requestObserver.onCompleted();

        try {
            channel.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("Hello, fileuploadclient!");
        fileuploadclient fileuploadclient = new fileuploadclient("localhost", 50093);
        fileuploadclient.UPLOAD("/Users/zgw/image/0.png");

    }

}
